package cn.itcast.io.test;

import java.io.File;
import java.util.Arrays;

public class DeleteFile {

    public static void main(String[] args) {

        // 需求1：删除一个带内容的目录
        test1();

        //tempTest1();
    }


    private static void test1() {
        File file = new File("C:\\Users\\hz21076971\\Desktop\\Desktop\\picture - 副本");
        /**
         * 思路：
         * 1. 如果是文件对象，则直接删除，否则
         * 2. 循环判断目录下的内容，
         */
        deleteFile(file);

    }

    // 递归删除文件夹
    private static void deleteFile(File dir) {
        if(dir == null) return;
        if(!dir.exists()) return;
        if(!dir.isDirectory()){
            dir.delete();
        }
        File[] files = dir.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()) {
                deleteFile(file1);
            } else {
                System.out.println("file1:" + file1.delete());
            }
        }
        System.out.println("dir:" + dir.delete());
    }

    private static void tempTest1() {
        File file = new File("C:\\Users\\hz21076971\\Desktop\\Desktop\\picture\\小组学习目标2 - 副本.jpg");
        File[] files = file.listFiles();
        System.out.println(files);

    }
}
